<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>计算属性的set和get写法</title>
</head>
<body>
  <div id="app">
    <!--1. computed-->
    <h2>{{fullName}}</h2>
    <h2>{{fullName}}</h2>
    <h2>{{fullName}}</h2>
    <!--2. 修改name的值-->
    <button @click="setFullName()">修改name的值</button>
  </div>
  <script src="../lib/vue.js"></script>
  <script>
    // 使用vue
    const app = Vue.createApp({
      template: ``
      ,
      data: function(){
        return {
          firstName: "kobe",
          lastName: "jjj"
        }
      },
      methods: {
        setFullName() {
          console.log("computed里面的属性fullName: ", this.fullName)
          //console.log(this.fullName)
          this.fullName = "aaaa bbbb"
        }
      },
      computed: {
        // 只是一种语法糖, 只有get属性时,可以使用语法糖
        // fullName: function() {
        //   console.log("computed----------------------")
        //   return this.firstName + " " + this.lastName
        // },
        fullName: {
          get: function() {
            return this.firstName + " " + this.lastName
          },
          set: function(value) {
            console.log("set里的参数: " + value)
            const names = value.split(" ")
            this.firstName = names[0]
            console.log("修改后的firstName: " + this.firstName)
            this.lastName = names[1] || "" // 处理没有第二个名的情况
            console.log("修改后的lastName: " + this.lastName)
          }
        }
      }
    }
    )
    // 挂载 
    app.mount("#app")
  </script>

</body>
</html>
